<template>
  <div class="studentList">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input
          size="small"
          v-model="formInline.name"
          placeholder="请输入名字查询"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" @click="find" type="primary">查询</el-button>
        <el-button size="small" @click="reset" type="primary">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 1: 1-10 2: 11-20 slice((当前页数 - 1)*每页条数, 当前页数*当前条数)-->
    <el-table :data="countData" stripe border style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="sex" label="性别" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.sex === 1">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" align="center">
      </el-table-column>
      <el-table-column prop="number" label="学号" align="center">
      </el-table-column>
      <el-table-column prop="class" label="班级" align="center">
      </el-table-column>
      <el-table-column prop="state_text" label="状态" align="center">
        <!-- <template #default="{ row }">
          <span v-if="row.state === '1'">已入学</span
          ><span v-else-if="row.state === '2'">未入学</span
          ><span v-else>休学中</span>
        </template> -->
      </el-table-column>
      <el-table-column prop="address" label="地址" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="联系方式" align="center">
      </el-table-column>
      <el-table-column prop="address" align="center" label="操作">
        <template #default="{ row }"
          ><el-button
            @click="del(row.id)"
            type="danger"
            icon="el-icon-delete"
            circle
          ></el-button
        ></template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getStudents, deleteStudent } from "@/api/student";
export default {
  name: "StudentList",
  created() {
    this.ongetStudents();
  },
  data() {
    return {
      tableData: [], //表格数据
      currentPage: 1, //当前页数
      pageSize: 10, //每页显示条数
      formInline: {
        //查询的关键字
        name: "",
      },
      flag: false, //判断是否调用了查询函数
    };
  },
  computed: {
    countData() {
      return this.flag === true
        ? this.tableData
        : this.tableData.slice(
            (this.currentPage - 1) * this.pageSize,
            this.currentPage * this.pageSize
          );
    },
  },
  watch: {
    countData(newVal, oldVal) {
      // console.log(newVal);
      this.flag = false;
    },
  },
  methods: {
    // 获取列表数据
    async ongetStudents(params) {
      const { data } = await getStudents(params);
      this.tableData = data.data;
      if (!this.tableData.length) {
        this.$message.info("查无此人");
      }
      this.tableData.forEach((item) => {
        item.state === "1"
          ? (item.state_text = "已入学")
          : item.state === "2"
          ? (item.state_text = "未入学")
          : (item.state_text = "休学中");
      });
      // console.log(data.data);
    },
    // 分页
    // 当每页条数发生改变时
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
    },
    // 当前页数发生改变时
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    // 删除学生信息
    async del(id) {
      const { data } = await deleteStudent(id);
      if (data.status !== 200) return this.$message.error("数据删除失败");
      this.$message.success(data.message);
      this.tableData = this.tableData.filter((item) => {
        return item.id !== id;
      });
    },
    //查询学生信息
    find() {
      // console.log(this.formInline);
      this.ongetStudents(this.formInline);
      this.flag = true;
    },
    // 重置信息
    reset() {
      this.formInline = {};
      this.ongetStudents(this.formInline);
    },
  },
};
</script>

<style lang="less" scoped>
.studentList {
  .el-pagination {
    text-align: left;
    margin-top: 20px;
  }
  .demo-form-inline {
    text-align: left;
  }
}
</style>